#!/usr/bin/env python
    ##########################################################################
    ##(c) 2008 Bastian Damman                                               ##
    ##                                                                      ##
    ##This file is part of Kaboom-O-rama.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is free software: you can redistribute it and/or modify ##
    ##it under the terms of the GNU General Public License as published by  ##
    ##the Free Software Foundation, either version 3 of the License, or     ##
    ##(at your option) any later version.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is distributed in the hope that it will be useful,      ##
    ##but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
    ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
    ##GNU General Public License for more details.                          ##
    ##                                                                      ##
    ##You should have received a copy of the GNU General Public License     ##
    ##along with Kaboom-O-rama.  If not, see <http://www.gnu.org/licenses/>.##
    ##########################################################################
import math

class GridItem:
    "all the objects on the grid, things to save"
    def save(self):
        pass
    def draw(self, camera):
        camera.blit(self.img, self.pos)

class Grid:
    "this class draws the grid and holds all the content placed on the grid"
    def __init__(self, width, height, cel_size):
        self.min_height = 480
        self.min_width = 640
        self.new(width, height, cel_size)
        
    def new(self, width, height, cel_size):
        self.grid = [[[] for y in range(0, int(height / cel_size))] for x in range(0, int(width / cel_size))]
        self.cel_size = cel_size
        self.width = width
        self.height = height
    def add(self, pos, tool):
        pos = self.snap(pos)
        type = tool.type
        if pos[0] < 0 or pos[0] >= self.width:
            return
        if pos[1] < 0 or pos[1] >= self.height:
            return
        for obj in self.grid[int(pos[0] / self.cel_size)][int(pos[1] / self.cel_size)]:
            if obj != None:
                if obj.type == type:
                    return
        item = GridItem()
        item.type = type
        item.pos = pos
        item.img = None
        self.grid[int(pos[0]/self.cel_size)][int(pos[1]/self.cel_size)].append(tool.get_new_item(item))
        
    def clear(self, x = None, y = None, width = 32, height = 32, type = None):
        if x == None:
            self.new(self.width, self.height, self.cel_size)
        else:
            pos = self.snap((x, y))
            dim = self.snap((width, height))
            for xxx in range(pos[0], pos[0] + dim[0], self.cel_size):
                for yyy in range(pos[1], pos[1] + dim[1], self.cel_size):
                    if xxx < 0 or xxx >= self.width or yyy < 0 or yyy >= self.height:
                        continue
                    if type == None:
                        self.grid[int(xxx / self.cel_size)][int(yyy / self.cel_size)] = []
                    else:
                        for i in self.grid[int(xxx / self.cel_size)][int(yyy / self.cel_size)]:
                            if i.type == type:
                                self.grid[int(xxx / self.cel_size)][int(yyy / self.cel_size)].remove(i)
                
    
    def get_width(self):
        return self.width
    
    def get_height(self):
        return self.height
       
    def change_width(self, new_width):
        new_width = self.snap((new_width, 0))[0]
        if new_width < self.min_width:
            new_width = self.min_width
        if self.width == new_width:
            return
        if self.width < new_width:
            for x in range(0, int((new_width - self.width) / self.cel_size)):
                self.grid.append([[] for y in range(0, int(self.height / self.cel_size))])
            self.width = new_width
        else:
            for i in range(0, self.width - new_width, self.cel_size):
                del self.grid[-1]
            self.width = new_width
        return new_width
    def change_height(self, new_height):
        new_height = self.snap((0, new_height))[1]
        if new_height < self.min_height:
            new_height = self.min_height
        if self.height == new_height:
            return
        if self.height < new_height:
            for x in self.grid:
                for y in range(0, int((new_height - self.height) / self.cel_size)):
                    x.append([])
            self.height = new_height
        else:
            for x in self.grid:
                for i in range(0, self.height - new_height, self.cel_size):
                    del x[-1]
            self.height = new_height
        return new_height
    
    def change_size(self, new_width, new_height):
        a =  self.change_width(new_width)
        b = self.change_height(new_height)
        return (a, b)
    
    def snap(self, pos):
        x = int((pos[0] / self.cel_size) * self.cel_size)
        y = int((pos[1] / self.cel_size) * self.cel_size)
        return (x, y)
    
    def draw(self, camera):
        cam_pos = self.snap(camera.get_pos())
        
        #-----------draw grid-----------#
        for x in range(0, self.width, self.cel_size):           
            if x % 128 == 0: color = (225, 225, 225)
            else: color = (200, 200, 200)   
            camera.blit_line(x, 0, x, self.height, color)
        camera.blit_line(self.width, 0, self.width, self.height,
                         (255, 255, 255, 128))
        for y in range(0, self.height, self.cel_size):
            if y % 128 ==  0: color = (225, 225, 225)
            else: color = (200, 200, 200)
            camera.blit_line(0, y, self.width, y, color)
        camera.blit_line(0, self.height, self.width, self.height,
                         (255, 255, 255, 128))
        
        #-----------draw items-----------#
        for xxx in range(0, int(self.width/self.cel_size)):
            for yyy in range(0, int(self.height/self.cel_size)):
                for obj in self.grid[xxx][yyy]:
                    if obj != None:
                        obj.draw(camera)